﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Maticsoft.DAL
{
	/// <summary>
	/// 数据访问类:documentFlowInfo
	/// </summary>
	public partial class documentFlowInfo
	{
		public documentFlowInfo()
		{}
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("documentFlowInfo_id", "documentFlowInfo");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int documentFlowInfo_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from documentFlowInfo");
            strSql.Append(" where documentFlowInfo_id=@documentFlowInfo_id");
            SqlParameter[] parameters = {
					new SqlParameter("@documentFlowInfo_id", SqlDbType.Int,4)
			};
            parameters[0].Value = documentFlowInfo_id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Maticsoft.Model.documentFlowInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into documentFlowInfo(");
            strSql.Append("documentFlowInfo_name,document_id,flow_ids,flow_id,flow_name,flow_node_id_now,flow_node_num_now,status,isDone,documentFlowMapping_name,isFlowDone)");
            strSql.Append(" values (");
            strSql.Append("@documentFlowInfo_name,@document_id,@flow_ids,@flow_id,@flow_name,@flow_node_id_now,@flow_node_num_now,@status,@isDone,@documentFlowMapping_name,@isFlowDone)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@documentFlowInfo_name", SqlDbType.VarChar,100),
					new SqlParameter("@document_id", SqlDbType.Int,4),
					new SqlParameter("@flow_ids", SqlDbType.VarChar,100),
					new SqlParameter("@flow_id", SqlDbType.Int,4),
					new SqlParameter("@flow_name", SqlDbType.VarChar,100),
					new SqlParameter("@flow_node_id_now", SqlDbType.Int,4),
					new SqlParameter("@flow_node_num_now", SqlDbType.Int,4),
					new SqlParameter("@status", SqlDbType.Int,4),
					new SqlParameter("@isDone", SqlDbType.Bit,1),
					new SqlParameter("@documentFlowMapping_name", SqlDbType.VarChar,100),
					new SqlParameter("@isFlowDone", SqlDbType.Bit,1)};
            parameters[0].Value = model.documentFlowInfo_name;
            parameters[1].Value = model.document_id;
            parameters[2].Value = model.flow_ids;
            parameters[3].Value = model.flow_id;
            parameters[4].Value = model.flow_name;
            parameters[5].Value = model.flow_node_id_now;
            parameters[6].Value = model.flow_node_num_now;
            parameters[7].Value = model.status;
            parameters[8].Value = model.isDone;
            parameters[9].Value = model.documentFlowMapping_name;
            parameters[10].Value = model.isFlowDone;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Maticsoft.Model.documentFlowInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update documentFlowInfo set ");
            strSql.Append("documentFlowInfo_name=@documentFlowInfo_name,");
            strSql.Append("document_id=@document_id,");
            strSql.Append("flow_ids=@flow_ids,");
            strSql.Append("flow_id=@flow_id,");
            strSql.Append("flow_name=@flow_name,");
            strSql.Append("flow_node_id_now=@flow_node_id_now,");
            strSql.Append("flow_node_num_now=@flow_node_num_now,");
            strSql.Append("status=@status,");
            strSql.Append("isDone=@isDone,");
            strSql.Append("documentFlowMapping_name=@documentFlowMapping_name,");
            strSql.Append("isFlowDone=@isFlowDone");
            strSql.Append(" where documentFlowInfo_id=@documentFlowInfo_id");
            SqlParameter[] parameters = {
					new SqlParameter("@documentFlowInfo_name", SqlDbType.VarChar,100),
					new SqlParameter("@document_id", SqlDbType.Int,4),
					new SqlParameter("@flow_ids", SqlDbType.VarChar,100),
					new SqlParameter("@flow_id", SqlDbType.Int,4),
					new SqlParameter("@flow_name", SqlDbType.VarChar,100),
					new SqlParameter("@flow_node_id_now", SqlDbType.Int,4),
					new SqlParameter("@flow_node_num_now", SqlDbType.Int,4),
					new SqlParameter("@status", SqlDbType.Int,4),
					new SqlParameter("@isDone", SqlDbType.Bit,1),
					new SqlParameter("@documentFlowMapping_name", SqlDbType.VarChar,100),
					new SqlParameter("@isFlowDone", SqlDbType.Bit,1),
					new SqlParameter("@documentFlowInfo_id", SqlDbType.Int,4)};
            parameters[0].Value = model.documentFlowInfo_name;
            parameters[1].Value = model.document_id;
            parameters[2].Value = model.flow_ids;
            parameters[3].Value = model.flow_id;
            parameters[4].Value = model.flow_name;
            parameters[5].Value = model.flow_node_id_now;
            parameters[6].Value = model.flow_node_num_now;
            parameters[7].Value = model.status;
            parameters[8].Value = model.isDone;
            parameters[9].Value = model.documentFlowMapping_name;
            parameters[10].Value = model.isFlowDone;
            parameters[11].Value = model.documentFlowInfo_id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int documentFlowInfo_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from documentFlowInfo ");
            strSql.Append(" where documentFlowInfo_id=@documentFlowInfo_id");
            SqlParameter[] parameters = {
					new SqlParameter("@documentFlowInfo_id", SqlDbType.Int,4)
			};
            parameters[0].Value = documentFlowInfo_id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据(通过document_id删除)
        /// </summary>
        public bool DeleteList(string document_ids)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from documentFlowInfo ");
            strSql.Append(" where document_id in (" + document_ids + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Maticsoft.Model.documentFlowInfo GetModel(int document_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 documentFlowInfo_id,documentFlowInfo_name,document_id,flow_ids,flow_id,flow_name,flow_node_id_now,flow_node_num_now,status,isDone,documentFlowMapping_name,isFlowDone from documentFlowInfo ");
            strSql.Append(" where document_id=@document_id");
            SqlParameter[] parameters = {
					new SqlParameter("@document_id", SqlDbType.Int,4)
			};
            parameters[0].Value = document_id;

            Maticsoft.Model.documentFlowInfo model = new Maticsoft.Model.documentFlowInfo();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Maticsoft.Model.documentFlowInfo DataRowToModel(DataRow row)
        {
            Maticsoft.Model.documentFlowInfo model = new Maticsoft.Model.documentFlowInfo();
            if (row != null)
            {
                if (row["documentFlowInfo_id"] != null && row["documentFlowInfo_id"].ToString() != "")
                {
                    model.documentFlowInfo_id = int.Parse(row["documentFlowInfo_id"].ToString());
                }
                if (row["documentFlowInfo_name"] != null)
                {
                    model.documentFlowInfo_name = row["documentFlowInfo_name"].ToString();
                }
                if (row["document_id"] != null && row["document_id"].ToString() != "")
                {
                    model.document_id = int.Parse(row["document_id"].ToString());
                }
                if (row["flow_ids"] != null)
                {
                    model.flow_ids = row["flow_ids"].ToString();
                }
                if (row["flow_id"] != null && row["flow_id"].ToString() != "")
                {
                    model.flow_id = int.Parse(row["flow_id"].ToString());
                }
                if (row["flow_name"] != null)
                {
                    model.flow_name = row["flow_name"].ToString();
                }
                if (row["flow_node_id_now"] != null && row["flow_node_id_now"].ToString() != "")
                {
                    model.flow_node_id_now = int.Parse(row["flow_node_id_now"].ToString());
                }
                if (row["flow_node_num_now"] != null && row["flow_node_num_now"].ToString() != "")
                {
                    model.flow_node_num_now = int.Parse(row["flow_node_num_now"].ToString());
                }
                if (row["status"] != null && row["status"].ToString() != "")
                {
                    model.status = int.Parse(row["status"].ToString());
                }
                if (row["isDone"] != null && row["isDone"].ToString() != "")
                {
                    if ((row["isDone"].ToString() == "1") || (row["isDone"].ToString().ToLower() == "true"))
                    {
                        model.isDone = true;
                    }
                    else
                    {
                        model.isDone = false;
                    }
                }
                if (row["documentFlowMapping_name"] != null)
                {
                    model.documentFlowMapping_name = row["documentFlowMapping_name"].ToString();
                }
                if (row["isFlowDone"] != null && row["isFlowDone"].ToString() != "")
                {
                    if ((row["isFlowDone"].ToString() == "1") || (row["isFlowDone"].ToString().ToLower() == "true"))
                    {
                        model.isFlowDone = true;
                    }
                    else
                    {
                        model.isFlowDone = false;
                    }
                }
            }
            return model;
        }

       
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" documentFlowInfo_id,documentFlowInfo_name,document_id,flow_ids,flow_id,flow_name,flow_node_id_now,flow_node_num_now,status,isDone,documentFlowMapping_name,isFlowDone ");
            strSql.Append(" FROM documentFlowInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM documentFlowInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.documentFlowInfo_id desc");
            }
            strSql.Append(")AS Row, T.*  from documentFlowInfo T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "documentFlowInfo";
            parameters[1].Value = "documentFlowInfo_id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
		#region  ExtensionMethod
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM VdocumentInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

		#endregion  ExtensionMethod
	}
}

